var NetworkManager = function(socket,world){
    this.socket = socket;
    this.world = world;
    this.initEvents();
    this.refreshing = false;
}
NetworkManager.prototype = {

    initEvents : function(callback){
        var self = this;
        this.socket.on('new-user', function(personage) {
            console.log(personage);
            self.world.addPersonage(personage); // on ajout un personnage au monde
        });
        this.socket.on('move-perso', function(personage) {
            self.movePersonage(personage);
        });
        this.socket.on('disconnect-user', function(personage) {
            self.removePersonage(personage);
        });
    },

    getPersonage : function(callback){
        var self = this;
        this.socket.emit('get-personage',function(personage){
            self.world.addPersonage(personage);
            if (callback != null)
                callback();
        });
    },

    removePersonage : function(p){
        var indexToRemove = null;
        for(var index in this.world.personages){
            if (this.world.personages[index].id == p.id){
                indexToRemove = index;
                break;
            }
        }

        var perso  = this.world.personages.splice(indexToRemove,1);
        console.log("calling delete");
        console.log(perso);
        perso[0].delete();
        delete perso;
        self.world.drawer.engine.drawing.drawScene(true);
    },

    getPersonages : function(callback){
        var self = this;
        console.log("get perso");
        this.socket.emit('get-personages',function(personages){
            console.log("ok info");
            console.log(personages);
            for(var i in personages)
                self.world.addPersonage(personages[i]);

            if (callback != null)
                callback();
        });
    },

    refreshMap : function(callback){
        if (this.refreshing)
            return;
        this.refreshing = true;
        var self = this;
        this.socket.emit('refresh-map',function(map){
            self.world.setMap(map);
            self.refreshing = false;
            if (callback != null)
                callback();
        });
    },

    updatePersonage : function(callback){
        this.socket.emit('update-personage', this.world.getPersonage().stringify());
    },

    putWorldInfo: function(callback){
        var self = this;
        console.log("putting info");
        this.socket.emit('put-worldinfo',this.world.worldInfo,function(){
            console.log("ok info");
            if (callback != null)
                callback();
        });
    },

    getTile : function(id, callback){
        var self = this;
        this.socket.emit('get-tile',id,function(tile){

            if (callback != null)
                callback(tile);
        });
    },

    persistBuilding : function(building){
        var self = this;
        this.socket.emit('persist-building',building);
    },

    movePersonage : function(personage){

        var perso = this.world.getPersonageById(personage.id);
        console.log(personage);
        console.log(perso);
        var keys = { u : 0, d : 0, l: 0, r : 0};
        if (perso.x > personage.coordinateX)
            keys.l = 1;
        else{
            if (perso.x < personage.coordinateX){
                keys.r = 1;
            }else{
                if (perso.y > personage.coordinateY)
                    keys.u = 1;
                else{
                    if (perso.y < personage.coordinateY)
                        keys.d = 1;
                }
            }
        }
        console.log(personage);
        console.log(keys);
        this.world.eventManager.movePersonage(perso,keys);

    },

    getAllPlants : function(callback){
        this.socket.emit('get-plants',function(plants){
            callback(plants);
        });
    },

    getMaturity : function(callback){
        this.socket.emit('get-maturity',function(m){
            callback(m);
        });
    },

    getAllHealth : function(callback){
        this.socket.emit('get-health',function(h){
            callback(h);
        });
    },

    plant : function(p,callback){
        var self = this;
        this.socket.emit("plant",p,function(p){
            if (callback != undefined)
                callback(p);
        })
    },

    deletePlant : function(id){

        this.socket.emit("delete-plant",id);

    },

    saveTile : function(tile){

        this.socket.emit("save-tile",tile);

    },

    getAllUserPlants : function(callback){
        var self = this;
        console.log("called");
        this.socket.emit("get-userplant",function(p){
            callback(p);
        })
    }
}
